package org.leviatan.dataharbour.gui.swingcomponents;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.listeners.PersistenceResourceTreeListener;
import org.leviatan.dataharbour.gui.swingcomponents.views.PersistenceResourceCreatePanel;
import org.leviatan.dataharbour.gui.swingcomponents.views.PersistenceResourceDetailPanel;
import org.leviatan.dataharbour.gui.swingmodels.TreeMdlPersistenceResource;
import org.leviatan.dataharbour.gui.swingmodels.TreeNodeMdlPersistenceResource;


public class PersistenceResourceTab extends JPanel {

	private static final long serialVersionUID = 5226178848204332286L;
	
	private JSplitPane jSplitPane;
	
	private JScrollPane jScrollPaneLeft;
	
	private JScrollPane jScrollPaneRight;
	
	private JPanel treePanel;
	
	private JTree persResTree;
	
	
	public PersistenceResourceTab() {
		super();
		this.init();
	}
	
	private void init() {
		this.setSize(300, 200);
		this.setLayout(new BorderLayout());
		this.add(getJSplitPane());
		
	}
	
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setLeftComponent(getLeftJScrollPane());
			jSplitPane.setRightComponent(getRightJScrollPane());
		}
		return jSplitPane;
	}
	
	private JScrollPane getLeftJScrollPane() {
		if (jScrollPaneLeft == null) {
			jScrollPaneLeft = new JScrollPane(this.getTreePanel());
			//jScrollPaneLeft.setPreferredSize(new Dimension(300, 500));
		}
		return jScrollPaneLeft;
	}
	
	
	private JScrollPane getRightJScrollPane() {
		if (jScrollPaneRight == null) {
			
			jScrollPaneRight = new JScrollPane(getRightPanel());
			
		}
		return jScrollPaneRight;
	}
	
	private JPanel getRightPanel() {
		PersistenceResourceDetailPanel detailPanel =
			new PersistenceResourceDetailPanel();
		
		PersistenceResourceCreatePanel createPanel = 
			new PersistenceResourceCreatePanel();
		
		// IMPORTANT
		RuntimeContext.persistenceResourceDetailView = detailPanel;
		RuntimeContext.persistenceResourceCreatePanel = createPanel;
		
		createPanel.setVisible(false);
		createPanel.setEnabled(false);
		
		detailPanel.setVisible(true);
		detailPanel.setEnabled(true);
		
		GridBagConstraints top = new GridBagConstraints();
		top.anchor = GridBagConstraints.NORTHWEST;
		top.fill = GridBagConstraints.NONE;
		top.gridx = 0;
		top.gridy = 0;
		top.weighty = 1.0;
		top.weightx = 1.0;
		
		JPanel containingPanel = new JPanel();
		containingPanel.add(detailPanel, top);
		containingPanel.add(createPanel,top);
		
		return containingPanel;
	}
	
	private JPanel getTreePanel() {
		if (treePanel == null) {
			treePanel = new JPanel();
		}
		treePanel.setLayout(new GridBagLayout());
		
		this.persResTree = this.initPersResTree();
		RuntimeContext.persResTree = this.persResTree;
		persResTree.addTreeSelectionListener(
				new PersistenceResourceTreeListener(persResTree));
		
		GridBagConstraints tBarConstr = new GridBagConstraints();
		tBarConstr.anchor = GridBagConstraints.NORTHWEST;
		tBarConstr.fill = GridBagConstraints.BOTH;
		tBarConstr.gridx = 0;
		tBarConstr.gridy = 0;
		
		GridBagConstraints treeConstr = new GridBagConstraints();
		treeConstr.anchor = GridBagConstraints.NORTHWEST;
		treeConstr.insets = new Insets(5, 5, 5, 5);
		treeConstr.fill = GridBagConstraints.BOTH;
		treeConstr.gridx = 0;
		treeConstr.gridy = 1;
		treeConstr.weightx = 1.0;
		treeConstr.weighty = 1.0;
		
		treePanel.add(this.getToolBar(), tBarConstr);
		treePanel.add(this.persResTree, treeConstr);
			
		return treePanel;
	}
	
	private JTree initPersResTree() {
		
		if (persResTree == null ) {
			persResTree = new JTree();
			persResTree.setModel(new TreeMdlPersistenceResource());
			//ListenerFactory.initTreeSelectionListener(requestsTree);
			//jTree.setPreferredSize(size);
		}
		return persResTree;
		
	}
	
	private JToolBar getToolBar() {
		
		JToolBar tBar = new JToolBar();
		
		JButton butAdd = null;
		JButton butDelete = null;
		
		tBar.setLayout(new GridBagLayout());
		
		GridBagConstraints butCreateTblConstr = new GridBagConstraints();
		butCreateTblConstr.anchor = GridBagConstraints.WEST;
		butCreateTblConstr.insets = new Insets(0, 5, 0, 5);
		butCreateTblConstr.gridx = 0;
		butCreateTblConstr.gridy = 0;
		butCreateTblConstr.fill = GridBagConstraints.BOTH;
	
		GridBagConstraints butDeleteConstr = new GridBagConstraints();
		butDeleteConstr.anchor = GridBagConstraints.WEST;
		butDeleteConstr.insets = new Insets(0, 0, 0, 5);
		butDeleteConstr.gridx = 3;
		butDeleteConstr.gridy = 0;
		butDeleteConstr.weightx = 1.0;
		butDeleteConstr.weighty = 1.0;
		
		GridBagConstraints butCreateRecConstr = new GridBagConstraints();
		butCreateRecConstr.anchor = GridBagConstraints.WEST;
		butCreateRecConstr.insets = new Insets(0, 0, 0, 5);
		butCreateRecConstr.gridx = 1;
		butCreateRecConstr.gridy = 0;
		
		GridBagConstraints butCreateFldConstr = new GridBagConstraints();
		butCreateFldConstr.anchor = GridBagConstraints.WEST;
		butCreateFldConstr.insets = new Insets(0, 0, 0, 5);
		butCreateFldConstr.gridx = 2;
		butCreateFldConstr.gridy = 0;
		
		
		butAdd = new JButton("New");
		butDelete = new JButton("Remove");
		
		butAdd.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				RuntimeContext.persistenceResourceCreatePanel.setVisible(true);
				RuntimeContext.persistenceResourceCreatePanel.setEnabled(true);
				
				RuntimeContext.persistenceResourceDetailView.setVisible(false);
				RuntimeContext.persistenceResourceDetailView.setEnabled(false);
			}
		});
		
		butDelete.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				String pId = getSelectedPRId();
				if (pId != null) {
					RuntimeContext.dataHarbourService.removePersistenceResource(pId);
					RuntimeContext.dataHarbourService.savePersistenceResources();
					
					((TreeMdlPersistenceResource) RuntimeContext.persResTree.getModel()).reload();
				}
			}
		});
		
		tBar.add(butAdd, butCreateTblConstr);
		tBar.add(butDelete, butDeleteConstr);
		
		tBar.setRollover(true);
		tBar.setFloatable(false);
		
		return tBar;
		
	}
	
	private String getSelectedPRId() {
		DefaultMutableTreeNode selectedPersRes = (DefaultMutableTreeNode) 
			RuntimeContext.persResTree.getLastSelectedPathComponent();

		if (selectedPersRes == null) return null;

		TreeNodeMdlPersistenceResource nodeModelPersRes = (TreeNodeMdlPersistenceResource) 
			selectedPersRes.getUserObject();
		
		return nodeModelPersRes.getId();
	}

}
